package app.lee.com.gesture;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import app.lee.com.view.GestureLockView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    GestureLockView gv;
    Button btnChange;
    String pwd = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnChange = (Button) findViewById(R.id.btnChangePwd);
        btnChange.setOnClickListener(this);
        SharedPreferences sp = this.getSharedPreferences("config", MODE_PRIVATE);
        gv = (GestureLockView) findViewById(R.id.gv);
        if (sp != null) {
            pwd = sp.getString("pwd", null);
        }

        if (pwd == null) {
            Toast.makeText(MainActivity.this, "未检测到密码，请先设置",
                    Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, ChangeActivity.class);
            startActivity(intent);
            return;
        }


        gv.setOnGestureFinishListener(new GestureLockView.OnGestureFinishListener() {

            @Override
            public void OnGestureFinish(boolean result) {

                if (result) {
                    Toast.makeText(MainActivity.this, "验证成功！",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "验证失败！",
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void OnSettingFinish(String result) {
                // TODO Auto-generated method stub

            }
        });

    }

    @Override
    protected void onResume() {
        SharedPreferences sp = this.getSharedPreferences("config", MODE_PRIVATE);
        gv = (GestureLockView) findViewById(R.id.gv);
        if (sp != null) {
            pwd = sp.getString("pwd", null);
        }
        gv.setKey(pwd);
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnChangePwd:
                Intent intent = new Intent(this, ChangeActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}
